package lumis.service.wsrp.consumer;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.xml.ws.Holder;

import lumis.portal.PortalException;

import com.sun.portal.wsrp.common.WSRPStubVersionConvertor;
import com.sun.portal.wsrp.common.stubs.V1DestroyFailed;
import com.sun.portal.wsrp.common.stubs.V1Extension;
import com.sun.portal.wsrp.common.stubs.V1ModelDescription;
import com.sun.portal.wsrp.common.stubs.V1PortletContext;
import com.sun.portal.wsrp.common.stubs.V1PortletDescription;
import com.sun.portal.wsrp.common.stubs.V1Property;
import com.sun.portal.wsrp.common.stubs.V1PropertyDescription;
import com.sun.portal.wsrp.common.stubs.V1PropertyList;
import com.sun.portal.wsrp.common.stubs.V1RegistrationContext;
import com.sun.portal.wsrp.common.stubs.V1ResetProperty;
import com.sun.portal.wsrp.common.stubs.V1ResourceList;
import com.sun.portal.wsrp.common.stubs.V1UserContext;
import com.sun.portal.wsrp.common.stubs.V1UserProfile;
import com.sun.portal.wsrp.common.stubs.v2.RegistrationContext;

/**
 * Helper for remote portlet version 1.
 *
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
class RemotePortletHelperV1 extends RemotePortletHelper
{
	RemotePortletHelperV1(String portletHandle, RegistrationContext registrationContext)
	{
		super(portletHandle, registrationContext);
	}
	
	Holder<List<V1Extension>> getEmptyPortletExtensionsHolderV1() throws PortalException
	{
		Holder<List<V1Extension>> portletExtensionsHolder = new Holder<List<V1Extension>>();
		portletExtensionsHolder.value = new ArrayList<V1Extension>();
		return portletExtensionsHolder;
	}
	
	Holder<List<V1Property>> getEmptyPropertiesHolderV1()
	{
		Holder<List<V1Property>> propertiesHolder = new Holder<List<V1Property>>();
		propertiesHolder.value = new ArrayList<V1Property>();
		return propertiesHolder;
	}
	
	Holder<List<V1ResetProperty>> getEmptyResetPropertyListHolderV1()
	{
		Holder<List<V1ResetProperty>> resetProperties = new Holder<List<V1ResetProperty>>();
		return resetProperties;
	}
	
	Holder<List<V1DestroyFailed>> getEmptyDestroyFailedListHolderV1()
	{
		Holder<List<V1DestroyFailed>> holder = new Holder<List<V1DestroyFailed>>();
		return holder;
	}
	
	/**
	 * Returns the registration context.
	 * @return	the registration context.
	 * @throws PortalException
	 */
	V1RegistrationContext getRegistrationContextV1() throws PortalException
	{
		return WSRPStubVersionConvertor.getV1RegistrationContext(this.registrationContext);
	}
	
	/**
	 * Returns the portlet context. 
	 * @return	the portlet context.
	 */
	V1PortletContext getPortletContextV1()
	{
		V1PortletContext portletContext = new V1PortletContext();
		portletContext.setPortletHandle(this.portletHandle);
		
		return portletContext;
	}
	
	/**
	 * Returns the user context.
	 * @return	the user context.
	 */
	V1UserContext getEmptyUserContextV1() 
	{
		V1UserContext userContext = new V1UserContext();
		V1UserProfile userProfile = new V1UserProfile();
		userContext.setProfile(userProfile);						
		return userContext;		
	}
	
	Holder<V1ModelDescription> getEmptyModelDescriptionHolderV1()
	{
		Holder<V1ModelDescription> modelDescriptionHolder = new Holder<V1ModelDescription>();
		return modelDescriptionHolder;
	}
	
	Holder<V1ResourceList> getEmptyResourceListHolderV1()
	{
		Holder<V1ResourceList> resourceListHolder = new Holder<V1ResourceList>();
		return resourceListHolder;
	}
	
	Holder<V1PortletDescription> getEmptyPortletDescriptionHolderV1()
	{
		Holder<V1PortletDescription> holder = new Holder<V1PortletDescription>();
		return holder;
	}
	
	static Properties toProperties(Holder<List<V1Property>> v1Properties)
	{
		Properties properties = new Properties();
		for (V1Property v1Property : v1Properties.value)
		{
			properties.put(v1Property.getName(), v1Property.getStringValue());
		}
		return properties;
	}
	
	static V1PropertyList toPropertyListV1(Properties properties)
	{
		V1PropertyList propertyList = new V1PropertyList();		
		for (Object key : properties.keySet())
		{
			V1Property v1Property = new V1Property();
			v1Property.setName((String) key);
			v1Property.setStringValue(properties.getProperty((String) key));
			
			propertyList.getProperties().add(v1Property);
		}
		return propertyList;
	}
	
	static String toPropertyDescription(Holder<V1ModelDescription> modelDescription, String propertyName)
	{
		if (modelDescription != null)
		{
			for (V1PropertyDescription propertyDescription: modelDescription.value.getPropertyDescriptions())
			{
				if (propertyDescription.getName().equals(propertyName))
				{
					return propertyDescription.getHint().getValue();
				}
			}
		}
		return null;
	}
}